package com.zhuhjay.boot.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuhjay.boot.mapper.MemberMapper;
import com.zhuhjay.boot.mapper.OrderMapper;
import com.zhuhjay.boot.mapper.OrdersettingMapper;
import com.zhuhjay.boot.mapper.SetmealMapper;
import com.zhuhjay.constant.MessageConstant;
import com.zhuhjay.domain.entity.Member;
import com.zhuhjay.domain.entity.Order;
import com.zhuhjay.domain.entity.OrderSetting;
import com.zhuhjay.domain.entity.Setmeal;
import com.zhuhjay.domain.http.Result;
import com.zhuhjay.service.OrderService;
import com.zhuhjay.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhuhjay
 * @since 2022-04-02
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result add(Map<String, Object> formData) throws Exception {
        String orderDate = (String) formData.get("orderDate");
        // 1. 判断用户预约的日期是否有指定可以预约
        OrderSetting orderSetting = ordersettingMapper.selectOne(
                new QueryWrapper<OrderSetting>()
                        .eq("orderDate", orderDate));
        if( orderSetting == null ){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        // 2. 判断用户预约的日期是否已经预约满员
        if( orderSetting.getReservations() >= orderSetting.getNumber() ){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        // 3. 判断用户是否重复预约
        Member member = memberMapper.selectOne(new QueryWrapper<Member>()
                            .eq("phoneNumber", formData.get("telephone")));
        if( member != null ){
            // 查询套餐, 时间, 用户id是否已经存在
            Order order = baseMapper.selectOne(new QueryWrapper<Order>()
                            .eq("setmeal_id", formData.get("setmealId"))
                            .eq("orderDate", orderDate)
                            .eq("member_id", member.getId()));
            if( order != null ){
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else{
            // 4. 检查当前预约用户是否是会员, 是就直接预约, 不是则自动注册并进行预约
            member = new Member();
            member.setPhoneNumber((String) formData.get("telephone"));
            member.setIdCard((String) formData.get("idCard"));
            member.setSex((String) formData.get("sex"));
            member.setName((String) formData.get("name"));
            member.setRegTime(new Date());
            memberMapper.insert(member);
        }
        // 5. 预约成功更新数据库的预约信息
        Order order = new Order();
        order.setMemberId(member.getId());
        order.setOrderDate(DateUtils.parseString2Date(orderDate));
        order.setSetmealId(Integer.parseInt((String) formData.get("setmealId")));
        order.setOrderType((String) formData.get("orderType"));
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        baseMapper.insert(order);

        ordersettingMapper.update(null,
                new UpdateWrapper<OrderSetting>()
                        .set("reservations", orderSetting.getReservations() + 1)
                        .eq("id", orderSetting.getId()));

        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map<String, Object> findInfoById(Integer id) {
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> map = new HashMap<>();
        // 获取预约信息
        Order order = baseMapper.selectById(id);
        // 获取会员信息
        Member member = memberMapper.selectById(order.getMemberId());
        // 获取套餐信息
        Setmeal setmeal = setmealMapper.selectById(order.getSetmealId());
        map.put("member", member.getName());
        map.put("setmeal", setmeal.getName());
        map.put("orderDate", df1.format(order.getOrderDate()));
        map.put("orderType", order.getOrderType());
        return map;
    }
}
